package com.lzb.permanentcalendar.activity;

import android.graphics.Color;
import android.icu.util.Calendar;
import android.os.Bundle;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.lzb.permanentcalendar.BaseAc;
import com.lzb.permanentcalendar.R;
import com.lzb.permanentcalendar.databinding.ActivityChineseZodiacQueryBinding;
import com.lzb.permanentcalendar.dialog.LunarTimeDialog;
import com.lzb.permanentcalendar.dialog.SelDateDialog;
import com.lzb.permanentcalendar.dialog.SolarTimeDialog;
import com.nlf.calendar.Lunar;
import com.nlf.calendar.Solar;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class ChineseZodiacQueryActivity extends BaseAc<ActivityChineseZodiacQueryBinding> implements View.OnClickListener {

    private int type = 0;
    private SolarTimeDialog solarTimeDialog;
    private LunarTimeDialog lunarTimeDialog;

    @Override
    protected int onCreate() {
        return R.layout.activity_chinese_zodiac_query;
    }

    @Override
    protected void initView() {
        mDataBinding.ivChineseZodiacQueryBack.setOnClickListener(view -> getOnBackPressedDispatcher().onBackPressed());

        mDataBinding.llLunar.setOnClickListener(this);
        mDataBinding.llSolar.setOnClickListener(this);
        mDataBinding.tvSelDate.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.llLunar) {//阴历
            clearSelection();
            mDataBinding.tvLunar.setTextColor(Color.parseColor("#AD2419"));
            mDataBinding.ivLunar.setVisibility(View.VISIBLE);
            type = 0;
        } else if (id == R.id.llSolar) {//阳历
            clearSelection();
            mDataBinding.tvSolar.setTextColor(Color.parseColor("#AD2419"));
            mDataBinding.ivSolar.setVisibility(View.VISIBLE);
            type = 1;
        } else if (id == R.id.tvSelDate) {//选择日期
            if (type == 0) {
                showLunarTimeDialog();
            } else {
                showSolarTimeDialog();
            }
        }
    }

    @Override
    protected void initData() {
        Solar solar = Solar.fromDate(new Date());
        Lunar lunar = solar.getLunar();
        mDataBinding.tvSelDate.setText(lunar.getYear() + "年" + lunar.getMonthInChinese() + "月" + lunar.getDayInChinese() + lunar.getTimeInGanZhi() + "时");
        mDataBinding.tvDate.setText(getString(R.string.solar) + solar.getYear() + "年" + (solar.getMonth() < 10 ? "0" + solar.getMonth() : solar.getMonth()) + "月" + (solar.getDay() < 10 ? "0" + solar.getDay() : solar.getDay()) + "日" + (solar.getHour() < 10 ? "0" + solar.getHour() : solar.getHour()) + ":00");
        getData(solar.getYear(), solar.getMonth(), solar.getDay(), solar.getHour());
    }

    private void getData(int year, int month, int day, int hour) {
        try {
            // 1. 创建公历对象
            Solar solar = Solar.fromYmdHms(year, month, day, hour, 0, 0);
            Lunar lunar = solar.getLunar();

            // 2. 基础信息
            String yearGanZhi = lunar.getYearInGanZhi();       // 年干支（乙巳）
            String yearShengXiao = lunar.getYearShengXiao(); // 生肖（蛇）
            String monthGanZhi = lunar.getMonthInGanZhi();    // 月干支（辛巳）
            String dayGanZhi = lunar.getDayInGanZhi();         // 日干支（丁酉）
            String hourGanZhi = lunar.getTimeInGanZhi();         // 时干支（丁酉）
            String lunarDate = lunar.getMonthInChinese() + "月" + lunar.getDayInChinese(); // 农历五月初二


            // 3. 神位方位（需根据实际库字段名调整）
            String caiShen = lunar.getDayPositionCaiDesc();      // 财神方位// 财神方位
            String xiShen = lunar.getDayPositionXiDesc();        // 喜神方位
            String fuShen = lunar.getDayPositionFuDesc();        // 福神方位
            String yangGui = lunar.getDayPositionYangGuiDesc();  // 阳贵神方位

            // 4. 宜忌事项
            List<String> yiList = lunar.getDayYi();          // 宜事项列表
            List<String> jiList = lunar.getDayJi();         // 忌事项列表
            String yi = String.join(" ", yiList);
            String ji = String.join(" ", jiList);

            // 5. 格式化输出
            System.out.println("公历日期：" + solar);
            System.out.println("干支信息：" + yearGanZhi + "·" + yearShengXiao + "年 "
                    + monthGanZhi + "月 " + dayGanZhi + "日");
            System.out.println("农历日期：" + lunarDate);
            System.out.println("神位方位：财神 " + caiShen
                    + "，喜神 " + xiShen
                    + "，福神 " + fuShen
                    + "，阳贵 " + yangGui);
            System.out.println("宜：" + yi);
            System.out.println("忌：" + ji);

            mDataBinding.tvYear.setText(yearGanZhi + "年 " + getString(R.string.chinese_zodiac_sign) + lunar.getYearShengXiao());
            mDataBinding.tvMonth.setText(monthGanZhi + "月 " + getString(R.string.chinese_zodiac_sign) + lunar.getMonthShengXiao());
            mDataBinding.tvDay.setText(dayGanZhi + "日 " + getString(R.string.chinese_zodiac_sign) + lunar.getDayShengXiao());
            mDataBinding.tvHour.setText(hourGanZhi + "时 " + getString(R.string.chinese_zodiac_sign) + lunar.getTimeShengXiao());

            Solar d = Solar.fromYmd(2025, 5, 12);
            List<String> l = d.getFestivals();
            for (String s : l) {
                System.out.println(s);
            }

            d = Solar.fromYmd(2025, 5, 12);
            l = d.getOtherFestivals();
            for (String s : l) {
                System.out.println(s);
            }
        } catch (Exception e) {
            System.err.println("日期解析错误：" + e.getMessage());
        }
    }

    private void showLunarTimeDialog() {
        if (lunarTimeDialog == null) lunarTimeDialog = new LunarTimeDialog(mContext);
        lunarTimeDialog.setListener(new LunarTimeDialog.Ilistener() {
            @Override
            public void clickConfirm(int year, int mo, int days, int hour) {
                Lunar lunar = new Lunar(year, mo, days,hour,0,0);
                // 转换为阳历
                Solar solar = lunar.getSolar();
                mDataBinding.tvSelDate.setText(lunar.getYear() + "年" + lunar.getMonthInChinese() + "月" + lunar.getDayInChinese() + lunar.getTimeInGanZhi() + "时");
                mDataBinding.tvDate.setText(getString(R.string.solar) + solar.getYear() + "年" + (solar.getMonth() < 10 ? "0" + solar.getMonth() : solar.getMonth()) + "月" + (solar.getDay() < 10 ? "0" + solar.getDay() : solar.getDay()) + "日" + (solar.getHour() < 10 ? "0" + solar.getHour() : solar.getHour()) + ":00");
                getData(solar.getYear(), solar.getMonth(), solar.getDay(), solar.getHour());
            }
        });
        lunarTimeDialog.show();
    }

    private void showSolarTimeDialog() {
        if (solarTimeDialog == null) solarTimeDialog = new SolarTimeDialog(mContext);
        solarTimeDialog.setListener(new SolarTimeDialog.Ilistener() {
            @Override
            public void clickConfirm(int year, int mo, int days, int hour) {
                Solar solar = Solar.fromYmdHms(year, mo, days, hour, 0, 0);
                Lunar lunar = solar.getLunar();
                mDataBinding.tvSelDate.setText(solar.getYear() + "年" + (solar.getMonth() < 10 ? "0" + solar.getMonth() : solar.getMonth()) + "月" + (solar.getDay() < 10 ? "0" + solar.getDay() : solar.getDay()) + "日" + (solar.getHour() < 10 ? "0" + solar.getHour() : solar.getHour()) + ":00");
                mDataBinding.tvDate.setText(lunar.getYearInChinese() + "年" + lunar.getMonthInChinese() + "月" + lunar.getDayInChinese() + lunar.getTimeInGanZhi() + "时");
                getData(solar.getYear(), solar.getMonth(), solar.getDay(), solar.getHour());
            }
        });
        solarTimeDialog.show();
    }

    private void clearSelection() {
        mDataBinding.tvLunar.setTextColor(Color.parseColor("#999999"));
        mDataBinding.ivLunar.setVisibility(View.GONE);
        mDataBinding.tvSolar.setTextColor(Color.parseColor("#999999"));
        mDataBinding.ivSolar.setVisibility(View.GONE);
    }

}